package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SuperSearchHistory;
import com.ruoyi.system.service.ISuperSearchHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 搜索历史Controller
 * 
 * @author ruoyi
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/search/history")
public class SuperSearchHistoryController extends BaseController
{
    @Autowired
    private ISuperSearchHistoryService superSearchHistoryService;

    /**
     * 查询搜索历史列表
     */
    @PreAuthorize("@ss.hasPermi('search:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(SuperSearchHistory superSearchHistory)
    {
        startPage();
        List<SuperSearchHistory> list = superSearchHistoryService.selectSuperSearchHistoryList(superSearchHistory);
        return getDataTable(list);
    }

    /**
     * 导出搜索历史列表
     */
    @PreAuthorize("@ss.hasPermi('search:history:export')")
    @Log(title = "搜索历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SuperSearchHistory superSearchHistory)
    {
        List<SuperSearchHistory> list = superSearchHistoryService.selectSuperSearchHistoryList(superSearchHistory);
        ExcelUtil<SuperSearchHistory> util = new ExcelUtil<SuperSearchHistory>(SuperSearchHistory.class);
        util.exportExcel(response, list, "搜索历史数据");
    }

    /**
     * 获取搜索历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('search:history:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(superSearchHistoryService.selectSuperSearchHistoryById(id));
    }

    /**
     * 新增搜索历史
     */
    @PreAuthorize("@ss.hasPermi('search:history:add')")
    @Log(title = "搜索历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SuperSearchHistory superSearchHistory)
    {
        return toAjax(superSearchHistoryService.insertSuperSearchHistory(superSearchHistory));
    }

    /**
     * 修改搜索历史
     */
    @PreAuthorize("@ss.hasPermi('search:history:edit')")
    @Log(title = "搜索历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SuperSearchHistory superSearchHistory)
    {
        return toAjax(superSearchHistoryService.updateSuperSearchHistory(superSearchHistory));
    }

    /**
     * 删除搜索历史
     */
    @PreAuthorize("@ss.hasPermi('search:history:remove')")
    @Log(title = "搜索历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(superSearchHistoryService.deleteSuperSearchHistoryByIds(ids));
    }

    // 根据用户id删除用户的搜索记录
    @PreAuthorize("@ss.hasPermi('search:history:remove')")
    @DeleteMapping("/deleteByUserId/{userId}")
    public AjaxResult removeByUserId(@PathVariable Long userId)
    {
        return toAjax(superSearchHistoryService.deleteByUserId(userId));
    }

}
